﻿/// Write a method that adds two positive integer numbers represented as arrays of digits (each array element arr[i] contains a digit;
/// the last digit is kept in arr[0]). Each of the numbers that will be added could have up to 10 000 digits.


using System;

namespace c_sh_II_lek_3
{
    class Zad8
    {
        static void Main()
        {
            int[] array1 = { 9, 1, 1, 2 };
            int[] array2 = { 3, 6, 9 };

            int[] bigerArr = GetBiggerArray(array1, array2);
            int[] smalerArr = GetSmalerArray(array1, array2);

            int[] result = new int[bigerArr.Length+1];
            int temp = 0;
            int leftOver = 0;
            int indexCount = 0;

            for (int i = 0; i < bigerArr.Length; i++)
            {
                if (i < smalerArr.Length)
                {
                    temp = bigerArr[i] + smalerArr[i];
                    setSum(result, temp, ref leftOver, i);
                }
                else
                {
                    temp = bigerArr[i] + 0;
                    setSum(result, temp, ref leftOver, i);
                }

                indexCount++;
            }

            if (leftOver == 1)
            {
                result[indexCount ] += leftOver;
            }

            for (int i = result.Length - 1; i >= 0; i--)
            {
                if (i == result.Length - 1 && result[i] == 0)
                {
                    continue;
                }
                else
                {
                    Console.Write(result[i]);
                }
               
            }
            Console.WriteLine();
            
        }

        private static void setSum(int[] result, int temp, ref int leftOver, int i)
        {
            if (temp > 9)
            {
                result[i] = temp % 10 + leftOver;
                leftOver = 1;
            }
            else
            {
                result[i] = temp + leftOver;
                leftOver = 0;
            }
        }

        static int[] GetBiggerArray(int[] arr1, int[] arr2)
        {
            if (arr1.Length >= arr2.Length)
            {
                return arr1;
            }
            else
            {
                return arr2;
            }
        }

        static int[] GetSmalerArray(int[] arr1, int[] arr2)
        {
            if (arr1.Length < arr2.Length)
            {
                return arr1;
            }
            else
            {
                return arr2;
            }
        }
    }
}
